package cn.iune.common.base;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.alibaba.druid.pool.DruidDataSource;

@Repository
public abstract class BaseJPAImpl<T> {
	
//	private static final String CONN_JDBC = "java:/comp/env/baas";

//	@PersistenceContext(unitName="default")
	@PersistenceContext
	protected EntityManager em;

//	@Autowired
//	private BaasDataSource baasDataSource;
	@Autowired
	private DruidDataSource dataSource;

	public BaseJPAImpl() {
		super();
	}

	public Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}

	public long getCount(String sql, List<Object> params) {
		// TODO Auto-generated method stub
		
		Query query = em.createQuery(sql);
		//给参数赋值
		for(int i=0;i<params.size();i++){
//			log.info("params.get(i)-->"+params.get(i));
			query.setParameter(i+1, params.get(i));
		}
		
		long singleResult =  (long) query.getSingleResult();
		return singleResult;
	}

	public long getCount(String sql, Map<String,Object> params) {
		// TODO Auto-generated method stub
		
		Query query = em.createQuery(sql);
		//给参数赋值
		for (Map.Entry<String,Object> entry : params.entrySet()) { 
			  //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
			  query.setParameter(entry.getKey(), entry.getValue());
			}
		
		long singleResult =  (long) query.getSingleResult();
		return singleResult;
	}

	public /*List<Object>*/ List<T> query(String sql, List<Object> params, Integer offset, Integer limit) {
		// TODO Auto-generated method stub
		Query query = em.createQuery(sql);
		//给参数赋值
		for(int i=0;i<params.size();i++){
//			log.info("params.get(i)-->"+params.get(i));
			query.setParameter(i+1, params.get(i));
//			query.setParameter(i, params.get(i));
		}
		//分页
        /////////////分页//////////////////////////////////////////////
        if(limit != -1){
            query.setFirstResult(offset);
            query.setMaxResults(limit);
        }
        //////////////////////////////////////////////////////////////
		
		@SuppressWarnings("unchecked")
//		List<Object> resultList = query.getResultList();
		List<T> resultList = query.getResultList();
		return resultList;
	}
	

	public List<T> query(String sql, Map<String,Object> params, Integer offset, Integer limit) {
		// TODO Auto-generated method stub
		Query query = em.createQuery(sql);
		//给参数赋值
		for (Map.Entry<String,Object> entry : params.entrySet()) { 
		  //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
		  query.setParameter(entry.getKey(), entry.getValue());
		}
		//分页
        /////////////分页//////////////////////////////////////////////
        if(limit != -1){
            query.setFirstResult(offset);
            query.setMaxResults(limit);
        }
        //////////////////////////////////////////////////////////////
		
		@SuppressWarnings("unchecked")
//		List<Object> resultList = query.getResultList();
		List<T> resultList = query.getResultList();
		return resultList;
	}



}